<?php

/**
 * This is the model class for table "rs_user".
 *
 * The followings are the available columns in table 'rs_user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $last_visit
 * @property string $last_login
 * @property string $validation_code
 * @property integer $status
 * @property string $referal_key
 * @property integer $num_visits
 * @property string $ip_address
 * @property string $created_date
 * @property string $update_time
 * @property integer $validation_type
 * @property string $validation_expired
 * @property string $address1
 * @property string $address2
 * @property string $city
 * @property string $zip
 * @property string $state
 * @property string $country_id
 * @property string $google
 * @property string $twitter
 * @property string $linkedin
 * @property string $facebook
 *
 * The followings are the available model relations:
 * @property Appointment[] $appointments
 * @property AppointmentDiscusion[] $appointmentDiscusions
 * @property Business[] $businesses
 * @property Business[] $rsBusinesses
 * @property Service[] $services
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'rs_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status, num_visits, validation_type', 'numerical', 'integerOnly'=>true),
			array('username, email, validation_code, city, state, google, facebook', 'length', 'max'=>64),
			array('password', 'length', 'max'=>32),
			array('referal_key, zip', 'length', 'max'=>8),
			array('ip_address', 'length', 'max'=>16),
			array('address1, address2', 'length', 'max'=>255),
			array('country_id', 'length', 'max'=>5),
			array('twitter, linkedin', 'length', 'max'=>20),
			array('last_visit, last_login, created_date, update_time, validation_expired', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, email, last_visit, last_login, validation_code, status, referal_key, num_visits, ip_address, created_date, update_time, validation_type, validation_expired, address1, address2, city, zip, state, country_id, google, twitter, linkedin, facebook', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'appointments' => array(self::HAS_MANY, 'Appointment', 'consumer_id'),
			'appointmentDiscusions' => array(self::HAS_MANY, 'AppointmentDiscusion', 'user_id'),
			'businesses' => array(self::HAS_MANY, 'Business', 'created_by'),
			'rsBusinesses' => array(self::MANY_MANY, 'Business', 'rs_business_user(employee_id, business_id)'),
			'services' => array(self::HAS_MANY, 'Service', 'created_by'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'last_visit' => 'Last Visit',
			'last_login' => 'Last Login',
			'validation_code' => 'Validation Code',
			'status' => 'Status',
			'referal_key' => 'Referal Key',
			'num_visits' => 'Num Visits',
			'ip_address' => 'Ip Address',
			'created_date' => 'Created Date',
			'update_time' => 'Update Time',
			'validation_type' => 'Validation Type',
			'validation_expired' => 'Validation Expired',
			'address1' => 'Address1',
			'address2' => 'Address2',
			'city' => 'City',
			'zip' => 'Zip',
			'state' => 'State',
			'country_id' => 'Country',
			'google' => 'Google',
			'twitter' => 'Twitter',
			'linkedin' => 'Linkedin',
			'facebook' => 'Facebook',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('last_visit',$this->last_visit,true);
		$criteria->compare('last_login',$this->last_login,true);
		$criteria->compare('validation_code',$this->validation_code,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('referal_key',$this->referal_key,true);
		$criteria->compare('num_visits',$this->num_visits);
		$criteria->compare('ip_address',$this->ip_address,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('validation_type',$this->validation_type);
		$criteria->compare('validation_expired',$this->validation_expired,true);
		$criteria->compare('address1',$this->address1,true);
		$criteria->compare('address2',$this->address2,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('zip',$this->zip,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('country_id',$this->country_id,true);
		$criteria->compare('google',$this->google,true);
		$criteria->compare('twitter',$this->twitter,true);
		$criteria->compare('linkedin',$this->linkedin,true);
		$criteria->compare('facebook',$this->facebook,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}